

* Danish Election Study
* Match following:
/*
gen decrease_public_sector = V149		outcome1
gen decrease_defense_spending = V150	outcome2
gen decrease_inequality = V151			outcome7
gen private_healthcare = V152			outcome8
gen less_refugees = V154				outcome26
gen lower_taxes = V155					outcome4
gen environment_friendly = V156			outcome21
gen more_freedom_companies = V164		outcome29
gen less_aid = V204						outcome25
gen harder_punishment = V229			outcome17
gen danish_soldiers = V243				outcome34
gen skilled_migration = V262			outcome23
gen eu_attitude = V283					outcome30

*/



clear all

capture program drop store_est
program define store_est
	local indepvar = "`1'"
	matrix mat_`indepvar'_n[`2',`3']=e(N)
	matrix mat_`indepvar'_beta[`2',`3']=_b[temp_x]
	matrix mat_`indepvar'_se[`2',`3']=_se[temp_x]
	matrix mat_`indepvar'_var[`2',`3']=_se[temp_x]^2
	matrix mat_`indepvar'_t[`2',`3']=_b[temp_x]/_se[temp_x]
end

* SE of effect estimate average
capture program drop calc_SE
program define calc_SE

	local indepvar = "`1'"
	local model = `2'

	* Total sum of variances
	egen sum_var_`indepvar'_`model'=sum(mat_`indepvar'_var`model')
	
	* Second term
	local tempsum = 0
	local actualn = 0
	forvalues i=1(1)`3' {
		if mat_`indepvar'_se[`i',`model']!=. {
			local actualn = `actualn'+1
		}
		forvalues j=1(1)`3' {
			if `i'!=`j' & mat_`indepvar'_se[`i',`model']!=. & mat_`indepvar'_se[`j',`model']!=. {
				local tempsum = `tempsum' + `4'corr_mat[`i',`j'] * mat_`indepvar'_se[`i',`model'] * ///
					mat_`indepvar'_se[`j',`model'] * `4'flip_`indepvar'[`i']  * `4'flip_`indepvar'[`j']
			}
		}
	}
	
	* Total standard error
	gen se_`indepvar'_`model' = sqrt(`tempsum' + sum_var_`indepvar'_`model'[1])*(1/`actualn') in 1

end

cd "C:\Userdata\Shared\Dofiles\PrelDoFiles\PrelRafael\Quantifying bias\replication_code"
use "C:\Userdata\Shared\Dofiles\PrelDoFiles\PrelRafael\Quantifying bias\ext_data\des_2007.dta" 

* Dependents
gen decrease_public_sector = V149
	replace decrease_public_sector=. if decrease_public_sector>5
gen decrease_defense_spending = V150
	replace decrease_defense_spending=. if decrease_defense_spending>5
gen decrease_inequality = V151
	replace decrease_inequality=. if decrease_inequality>5
gen private_healthcare = V152
	replace private_healthcare=. if private_healthcare>5
gen less_refugees = V154
	replace less_refugees=. if less_refugees>5
gen lower_taxes = V155
	replace lower_taxes=. if lower_taxes>5
gen environment_friendly = V156
	replace environment_friendly=. if environment_friendly>5
gen more_freedom_companies = V164
	replace more_freedom_companies=1.5 if more_freedom_companies==3
	replace more_freedom_companies=. if more_freedom_companies==8
gen less_aid = V204
	replace less_aid=. if less_aid==8
gen harder_punishment = V229
	replace harder_punishment=. if harder_punishment==8
gen danish_soldiers = V243
	replace danish_soldiers=. if danish_soldiers==8
gen skilled_migration = V262
	replace skilled_migration=. if skilled_migration==8
gen eu_attitude = V283
	replace eu_attitude=. if eu_attitude>5

local i=1
foreach v of varlist decrease_public_sector - eu_attitude {
	gen outcome`i' = `v'
	local i = `i' + 1
}


* Gen independents
gen trust = V289
	replace trust=. if trust==8
gen educationYears=.
	replace educationYears=9 if V508==1
	replace educationYears=12 if V508==2
	replace educationYears=12 if V508==3
	replace educationYears=14 if V508==4
	replace educationYears=16 if V508==5
	replace educationYears=18 if V508==6
gen bin_edu=.
	replace bin_edu=0 if educationYears<16
	replace bin_edu=1 if educationYears>15 & educationYears!=.
gen income=.
	replace income = 50 if V427 == 1
	replace income = 117.5 if V427 == 2
	replace income = 137.5 if V427 == 3
	replace income = 162.5 if V427 == 4
	replace income = 187.5 if V427 == 5
	replace income = 225 if V427 == 6
	replace income = 275 if V427 == 7
	replace income = 325 if V427 == 8
	replace income = 375 if V427 == 9
	replace income = 425 if V427 == 10
	replace income = 475 if V427 == 11
	replace income = 550 if V427 == 12
	replace income = 650 if V427 == 13
	replace income = 750 if V427 == 14
	replace income = 900 if V427 == 15
	replace income = 1100 if V427 == 16

* Gen controls
gen sex = V279
gen age_cat = V475
gen municipality = V510
gen occupation = V443


* Inclusion
gen include_indicator=.
replace include_indicator=1 if sex!=. & age_cat!=. & municipality!=. & occupation!=. & income!= . & educationYears != .

* Cleanup
keep outcome1 - include_indicator


* Create necessary matrices for storage
foreach indepvar in bin_edu educationYears income trust {
	matrix mat_`indepvar'_beta=J(13,2,.)
	matrix mat_`indepvar'_n=J(13,2,.)
	matrix mat_`indepvar'_se=J(13,2,.)
	matrix mat_`indepvar'_var=J(13,2,.)
	matrix mat_`indepvar'_t=J(13,2,.)
}

* Correlation matrix
pwcorr outcome1-outcome13 if include_indicator==1
matrix corr_mat=r(C)


* Run analyses
local i=1
foreach depvar of varlist outcome1-outcome13 {
	
	foreach indepvar in educationYears bin_edu income trust {
	
		di `i' ", `depvar' `indepvar'"
	
		qui:su `depvar' if `indepvar'!=. & include_indicator!=.
		qui:gen temp_y = (`depvar'-r(mean))/r(sd)
		qui:su `indepvar' if `depvar'!=. & include_indicator!=.
		qui:gen temp_x = (`indepvar'-r(mean))/r(sd)
	
		* Empty model
		qui: reghdfe temp_y temp_x c.sex##i.age if include_indicator==1, noabsorb
		store_est `indepvar' `i' 1
	
		* Naive model
		if "`indepvar'" == "bin_edu"|"`indepvar'" == "educationYears" {
			qui: reghdfe temp_y temp_x c.sex##i.age i.occupation income if include_indicator==1, absorb(municipality)
			store_est `indepvar' `i' 2
		}
		if "`indepvar'" == "income" {
			qui: reghdfe temp_y temp_x c.sex##i.age i.occupation educationYears if include_indicator==1, absorb(municipality)
			store_est `indepvar' `i' 2
		}
		else {
			qui: reghdfe temp_y temp_x c.sex##i.age i.occupation educationYears income if include_indicator==1, absorb(municipality)
			store_est `indepvar' `i' 2
		}
		
		drop temp_x temp_y
		
	}
	local i=`i'+1
}

drop _all
foreach indepvar in bin_edu educationYears income trust {

	* Turn matrices into variables
	svmat mat_`indepvar'_n
	svmat mat_`indepvar'_beta
	svmat mat_`indepvar'_t
	svmat mat_`indepvar'_se
	svmat mat_`indepvar'_var
	
	* Flip vector
	gen flip_`indepvar' = 1
	replace flip_`indepvar' = -1 if mat_`indepvar'_beta1<0
	
	* Flipped betas
	gen `indepvar'_1 = mat_`indepvar'_beta1 * flip_`indepvar'
	gen `indepvar'_2 = mat_`indepvar'_beta2 * flip_`indepvar'
	
	* Means
	egen m_`indepvar'_1 = mean(`indepvar'_1)
	egen m_`indepvar'_2 = mean(`indepvar'_2)
	gen `indepvar'_m =.
	replace `indepvar'_m = m_`indepvar'_1[1] in 1
	replace `indepvar'_m = m_`indepvar'_2[1] in 2
	drop m_*
	
	* Standard errors
	calc_SE `indepvar' 1 13
	calc_SE `indepvar' 2 13
	gen `indepvar'_se =.
	replace `indepvar'_se = se_`indepvar'_1[1] in 1
	replace `indepvar'_se = se_`indepvar'_2[1] in 2

	gen `indepvar'_Nmin = .
	gen `indepvar'_Nmax = .
	gen `indepvar'_delta = .
	gen `indepvar'_tdelta = .
	forvalues i = 1(1)2 {
		egen Nmin_`indepvar'_`i' = min(mat_`indepvar'_n`i')
		egen Nmax_`indepvar'_`i' = max(mat_`indepvar'_n`i')
		replace `indepvar'_Nmin = Nmin_`indepvar'_`i'[1] in `i'
		replace `indepvar'_Nmax = Nmax_`indepvar'_`i'[1] in `i'
		drop Nmin_`indepvar'_`i' Nmax_`indepvar'_`i'
	}
	replace `indepvar'_delta = `indepvar'_m[2] - `indepvar'_m[1] in 2
	replace `indepvar'_tdelta = `indepvar'_delta[2]/sqrt(`indepvar'_se[1]^2 + `indepvar'_se[2]^2) in 2
	
}

keep *_m *_se *_Nmin *Nmax *_delta *_tdelta
keep if [_n]<3
rename * DES_*
save DES_est.dta, replace


